﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net;
using Doan_ver1.EMVService;
namespace Doan_ver1
{
    public partial class NapTienThe : System.Web.UI.Page
    {
        public string connection()
        {
            return Provider.connectionString();
        }
     

    
        public string generateRandomString()
        {
            string randomString = "";
            int randNumber;
            Random rnd = new Random();

            for (int i = 0; i < 6; i++)
            {
                if (rnd.Next(1, 3) == 1)
                    randNumber = rnd.Next(97, 103); //char {a-z}
                else
                    randNumber = rnd.Next(48, 58); //int {0-9}

                randomString = randomString + (char)randNumber;
            }
            return randomString;
        }
        public void load()
        {
            Label3.Text = generateRandomString();
            Session["ma"] = Label3.Text;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
                load();
        }
        protected int KiemTraHopLe()
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
            System.Net.ServicePointManager.Expect100Continue = false;
            EMVService.EMVServices proxy = new EMVServices();
           proxy.CookieContainer = new CookieContainer();
           The the = data.Thes.Where(c => c.MaThe == TextBox1.Text.Trim()).FirstOrDefault();
           if (the == null)
               return -1;
            string sID = proxy.Authenticate("NamABank", "b94DDu^xcC");


            int temp = proxy.CardValid2(sID, the.MaThe, the.MaLoaiThe, the.MaSecure, the.TenChuThe);
            return temp;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
           
            if (KiemTraHopLe() == 0)
            {
                The the = data.Thes.Single(c => c.MaThe == TextBox1.Text.Trim());
                Label1.Text = the.SoDu.ToString().Replace(".0000", string.Empty);
                Label2.Text = "Mã thẻ hợp lệ";
                
            }
            else
                Label2.Text = "Mã thẻ không hợp lệ";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SQLNganHangDataContext data = new SQLNganHangDataContext(connection());
            if (TextBox2.Text == Session["ma"] as string)
            {
                //   TaiKhoan tk = data.TaiKhoans.Single(c => c.TenDangNhap == Session["khachhang"] as string);
                The the = data.Thes.Single(c => c.MaThe == TextBox1.Text.Trim());
                //  tk.SoDuTK -= Convert.ToDecimal(TextBox3.Text);
                the.SoDu += Convert.ToDecimal(TextBox3.Text);
                data.SubmitChanges();
                Label4.Text = "Thành công";
                TextBox3.Text = null;
                TextBox2.Text = null;
                TextBox1.Text = null;
            }
            else
            {
                load();
                Label4.Text = "Thất bại";

            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            load();
        }
    }
}